package edu.sju.ssm;

import javax.sql.DataSource;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import edu.sju.ssm.config.DataAccessConfig;

public class DataSourceTest {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DataAccessConfig.class);
		
		DataSource dbcpDS = ctx.getBean("dataSource", DataSource.class);
		DataSource dmDS = ctx.getBean("springDataSource", DataSource.class);
		
		System.out.println(dbcpDS);
		System.out.println(dmDS);
		
		JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
		System.out.println(jdbcTemplate);
		
		Integer publisherCount = jdbcTemplate.queryForObject("select count(*) from publishers", Integer.class);
		System.out.printf("当前有 %d 个出版社%n", publisherCount);
		
		jdbcTemplate.update("INSERT INTO publishers (name, year_created, intro) values ('三江出版社', 2000, '内部专用')");
		
		publisherCount = jdbcTemplate.queryForObject("select count(*) from publishers", Integer.class);
		System.out.printf("当前有 %d 个出版社%n", publisherCount);
		
		ctx.close();
	}

}
